package com.nuanshui.heatedloan.credit.pay.behavior;

import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.credit.constant.OrderType;
import com.nuanshui.heatedloan.credit.pay.PayRequest;
import com.nuanshui.heatedloan.credit.pay.TransRequestCode;
import com.nuanshui.heatedloan.credit.pay.annotation.PayBehaviorAttribute;
import com.nuanshui.heatedloan.credit.pay.constant.PayBehaviorType;
import com.nuanshui.heatedloan.credit.pay.utils.AllinPayUtil;
import com.nuanshui.heatedloan.credit.pay.validation.ValidationResult;
import com.nuanshui.heatedloan.credit.pay.validation.ValidationResultCode;
import com.nuanshui.heatedloan.credit.vo.PayOrderInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 准备订单相关信息
 */
@PayBehaviorAttribute(value= PayBehaviorType.PrepareOrderMoneyBehavior,order=101)
public class PrepareOrderMoneyBehavior extends PayBehavior {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void internalInvoke(PayRequest request) {

        AllinPayUtil.logOrderPay(logger, request, "支付金额数据准备开始");

        PayOrderInfoVO payOrderInfoVO=request.getOrder();
        if(payOrderInfoVO==null){
            return;
        }
        //放款操作
        if( OrderType.APPROVEDORDER==payOrderInfoVO.getOrderStatus()){
            request.setMoney(payOrderInfoVO.getLoanAmount());  //贷款的金额
        }
        //还款操作
        if(OrderType.OVERDUEPAYMENT==payOrderInfoVO.getOrderStatus()||
                OrderType.NOREIMBURSEMENT==payOrderInfoVO.getOrderStatus()){
            request.setMoney(payOrderInfoVO.getOrderAmount()+payOrderInfoVO.getOverdueFee());
        }

        AllinPayUtil.logOrderPay(logger, request, "支付金额数据准备结束:"+request.getMoney());
    }

    @Override
    public void internalValidate(ValidationResult result, PayRequest request) {
        super.internalValidate(result, request);
        int payMoney=request.getMoney();
        if(payMoney<=0){
            result.setValidationResultCode(ValidationResultCode.NO_PAY_MONEY);
        }
    }
}
